package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.ProjectDTO;
import com.sanshi.project.admin.api.entity.CodeRuleEntity;
import com.sanshi.project.admin.api.entity.ProjectEntity;
import com.sanshi.project.assets.service.CodeRuleService;
import com.sanshi.project.assets.service.ProjectService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目
 *
 * @author nikon
 * @date 2023-11-16 23:48:43
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/project")
@Tag(description = "project", name = "项目管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProjectController {

    private final ProjectService projectService;
    private final CodeRuleService codeRuleService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param project 项目
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_project_view')")
    public R getProjectPage(@ParameterObject Page page, @ParameterObject ProjectDTO project) {
        LambdaQueryWrapper<ProjectEntity> wrapper = Wrappers.lambdaQuery();
        // 项目编号
        wrapper.like(StrUtil.isNotBlank(project.getCode()), ProjectEntity::getCode, project.getCode());
        // 项目名称
        wrapper.like(StrUtil.isNotBlank(project.getName()), ProjectEntity::getName, project.getName());
        // 区域
        wrapper.like(StrUtil.isNotBlank(project.getDistrict()), ProjectEntity::getDistrict, project.getDistrict());
        // 项目类型
        wrapper.in(ArrayUtil.isNotEmpty(project.getType()), ProjectEntity::getType, project.getType());
        // 产权归属
        wrapper.in(ArrayUtil.isNotEmpty(project.getOwnership()), ProjectEntity::getOwnership, project.getOwnership());
        // 产权单位
        wrapper.in(ArrayUtil.isNotEmpty(project.getUnit()), ProjectEntity::getUnit, project.getUnit());
        // 联系人
        wrapper.like(StrUtil.isNotBlank(project.getContact()), ProjectEntity::getContact, project.getContact());
        // 联系方式
        wrapper.like(StrUtil.isNotBlank(project.getTelephone()), ProjectEntity::getTelephone, project.getTelephone());
        return R.ok(projectService.page(page, wrapper));
    }


    /**
     * 通过id查询项目
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_project_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(projectService.getById(id));
    }

    /**
     * 新增项目
     *
     * @param project 项目
     * @return R
     */
    @Operation(summary = "新增项目", description = "新增项目")
    @SysLog("新增项目")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_project_add')")
    public R save(@RequestBody ProjectEntity project) {
        CodeRuleEntity codeRuleEntity = new CodeRuleEntity();
        codeRuleEntity.setType("10");
        R<String> resultCode = codeRuleService.generateCode(codeRuleEntity);
        if (resultCode.isOk()) {
            // 赋予项目编码
            project.setCode(resultCode.getData());
        } else {
            return resultCode;
        }
        return R.ok(projectService.save(project));
    }

    /**
     * 修改项目
     *
     * @param project 项目
     * @return R
     */
    @Operation(summary = "修改项目", description = "修改项目")
    @SysLog("修改项目")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_project_edit')")
    public R updateById(@RequestBody ProjectEntity project) {
        return R.ok(projectService.updateById(project));
    }

    /**
     * 通过id删除项目
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除项目", description = "通过id删除项目")
    @SysLog("通过id删除项目")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_project_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(projectService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param project 查询条件
     * @param ids     导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_project_export')")
    public List<ProjectEntity> export(ProjectEntity project, Long[] ids) {
        return projectService.list(Wrappers.lambdaQuery(project).in(ArrayUtil.isNotEmpty(ids), ProjectEntity::getId, ids));
    }

    /**
     * 项目全量查询
     *
     * @param project 项目管理
     * @return
     */
    @Operation(summary = "试验全量查询", description = "试验全量查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('assets_project_view')")
    public R getProjectList(@ParameterObject ProjectDTO project) {
        LambdaQueryWrapper<ProjectEntity> wrapper = Wrappers.lambdaQuery();
        // 项目编号
        wrapper.like(StrUtil.isNotBlank(project.getCode()), ProjectEntity::getCode, project.getCode());
        // 项目名称
        wrapper.like(StrUtil.isNotBlank(project.getName()), ProjectEntity::getName, project.getName());
        // 区域
        wrapper.like(StrUtil.isNotBlank(project.getDistrict()), ProjectEntity::getDistrict, project.getDistrict());
        // 项目类型
        wrapper.in(ArrayUtil.isNotEmpty(project.getType()), ProjectEntity::getType, project.getType());
        // 产权归属
        wrapper.in(ArrayUtil.isNotEmpty(project.getOwnership()), ProjectEntity::getOwnership, project.getOwnership());
        // 产权单位
        wrapper.in(ArrayUtil.isNotEmpty(project.getUnit()), ProjectEntity::getUnit, project.getUnit());
        return R.ok(projectService.list(wrapper));
    }
}